<template>
  <div class="img-list">
    <myheader titleName="图片展示"></myheader>
    <div class="cate">
      <ul id="cateul">
        <li @click="jumpto(0)"><span>全部</span></li>
        <li v-for="item in headlist" :key="item.list" @click="jumpto(item.id)"><span>{{item.title}}</span></li>
      </ul>
    </div>
    <div class="img-list-box">
      <ul>
        <li v-for="item in imglist" :key="item.id">
          <router-link :to="`/img/${item.id}/detail`"><img :src="item.img_url" alt="item.title"></router-link>
          <p>
            <span class="title">{{item.title}}</span>
            {{item.zhaiyao}}
          </p>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { getImgcategory, getImages } from '@/api'
import myheader from '@/components/myheader'
export default {
  components: {
    myheader
  },
  data () {
    return {
      headlist: [],
      imglist: []
    }
  },
  created () {
    this.initImglist()
    this.initImgs(0)
  },
  methods: {
    // 初始化头部列表
    initImglist () {
      getImgcategory()
        .then(res => {
          // console.log(res)
          this.headlist = res.message
          // 横向拖拉展示列表
          let len = res.message.length + 1
          let lenWidth = len * 60 + 'px'
          document.getElementById('cateul').style.width = lenWidth
        })
    },
    jumpto (id) {
      this.initImgs(id)
    },
    // 初始化全部图片列表
    initImgs (id) {
      getImages(id)
        .then(res => {
          console.log(res)
          this.imglist = res.message
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  .img-list {
    margin-bottom: 55px;
    margin-top: 40px;
  }
  .cate {
    max-width: 100%;
    margin: 0;
    overflow-x:auto;
    overflow-y: hidden;
    font-size: 14px;
    ul {
      padding-left: 10px;
      margin: 5px 0;
      li {
        list-style: none;
        display: inline-block;
        padding: 0 0 0 5px;
        span {
          color:#0094ff;
        }
      }
    }
  }
  .img-list-box {
    ul {
      padding: 0;
      li {
        list-style: none;
        position: relative;
      }
    }
    p {
      font-size: 14px;
      color: #fff;
      position: absolute;
      bottom: 0px;
      left: 0px;
      background-color: rgba(0,0,0,0.4);
      .title {
        font-weight: bold;
      }
    }
    img {
      width: 100%;
    }
  }

  image[lazy=loading] {
    width: 100%;
    margin: auto;
  }
</style>
